﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 设置容纳的线程数量
{
    class Program
    {
        /**
         * 当池子允许有3个元素的时候，就不让别的进来了，当有一个退出后，排队的就可以进去。
         * 
         * **/
        static Semaphore _semaphore = new Semaphore(3, 3);
        static void Main(string[] args)
        {
            Console.WriteLine("ladies and gentleman,舞会开始了~~");
            for (int i = 1; i <= 5; i++)
            {
                new Thread(IWannaDance).Start(i);
            }
            Console.Read();
        }

        static void IWannaDance(object id)
        {
            Console.WriteLine(id + "想跳舞");
            _semaphore.WaitOne();
            Console.WriteLine(id + "进了");
            Thread.Sleep(3000);
            Console.WriteLine(id + "准备离开舞池了");
            _semaphore.Release();

        }



    }
}
